//
//  WXPDeferRepaymentHeaderView.m
//  MexicoLoan
//
//  Created by wxp on 2025/3/19.
//

#import "WXPDeferRepaymentHeaderView.h"

@implementation WXPDeferRepaymentHeaderView

#pragma mark - lazy
- (UIView *)backView{
    if (_backView == nil) {
        _backView = [UIView new];
        _backView.widthHeight = XY(SCREEN_WIDTH-LEFT_MARDIN*2, 0);
        _backView.backgroundColor = COLOR_WHITE;
    }
    return _backView;
}
- (UIImageView *)bigImg{
    if (_bigImg == nil) {
        _bigImg = [UIImageView new];
        _bigImg.widthHeight = XY((36), (36));
        [GlobalMethod setRoundView:_bigImg color:[UIColor clearColor] numRound:(10) width:0];
    }
    return _bigImg;
}
- (UILabel *)nameLabel{
    if (_nameLabel == nil) {
        _nameLabel = [UILabel new];
        _nameLabel.textColor = COLOR_BLACK;
        _nameLabel.font = [UIFont boldSystemFontOfSize:(14)];
    }
    return _nameLabel;
}
- (UILabel *)stateLabel{
    if (_stateLabel == nil) {
        _stateLabel = [UILabel new];
        _stateLabel.textColor = COLOR_CONTENT;
        _stateLabel.fontNum = (12);
        _stateLabel.height = 20;
        [GlobalMethod setRoundView:_stateLabel color:[UIColor clearColor] numRound:20/2 width:0];
        _stateLabel.backgroundColor = COLOR_LINE;
        _stateLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _stateLabel;
}
- (UILabel *)moneyTitleLabel{
    if (_moneyTitleLabel == nil) {
        _moneyTitleLabel = [UILabel new];
        _moneyTitleLabel.textColor = COLOR_BLACK;
        _moneyTitleLabel.fontNum = (12);
    }
    return _moneyTitleLabel;
}
- (UILabel *)moneyLabel{
    if (_moneyLabel == nil) {
        _moneyLabel = [UILabel new];
        _moneyLabel.textColor = COLOR_BLACK;
        _moneyLabel.font = [UIFont boldSystemFontOfSize:(20)];
    }
    return _moneyLabel;
}
- (UILabel *)timetitleLabel{
    if (_timetitleLabel == nil) {
        _timetitleLabel = [UILabel new];
        _timetitleLabel.textColor = COLOR_BLACK;
        _timetitleLabel.fontNum = (12);
    }
    return _timetitleLabel;
}
- (UILabel *)timeLabel{
    if (_timeLabel == nil) {
        _timeLabel = [UILabel new];
        _timeLabel.textColor = COLOR_BLACK;
        _timeLabel.font = [UIFont boldSystemFontOfSize:(20)];
    }
    return _timeLabel;
}
- (UIButton *)btnOpen{
    if (!_btnOpen) {
        _btnOpen = [UIButton new];
        [_btnOpen setTitleColor:COLOR_LIGHT_GRAY forState:UIControlStateNormal];
        _btnOpen.titleLabel.fontNum = (12);
        NSString * str = @"Detalles de la factura";
        [_btnOpen setAttributedTitle:[str setImageName:@"icon-arrow-detail" imageRect:CGRectMake(0, -3, 14, 14) withIndex:str.length] forState:UIControlStateNormal];
        [_btnOpen sizeToFit];
        _btnOpen.widthHeight = XY(_btnOpen.width, _btnOpen.height+(8)*2);
        _btnOpen.selected = false;
        _btnOpen.tag = 1;
        [_btnOpen addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnOpen;
}
- (UIView *)fixedCopyView{
    if (_fixedCopyView == nil) {
        _fixedCopyView = [UIView new];
        _fixedCopyView.backgroundColor = [UIColor colorWithHexString:@"f6f6f6"];
        _fixedCopyView.width = self.backView.width-2*LEFT_MARDIN;
        [_fixedCopyView addSubview:self.fixedCopyLabel];
        self.fixedCopyLabel.leftTop = XY(LEFT_MARDIN, 12);
        _fixedCopyView.height = self.fixedCopyLabel.bottom+(12);
        [GlobalMethod setRoundView:_fixedCopyView color:[UIColor clearColor] numRound:16 width:0];
    }
    return _fixedCopyView;
}
- (UILabel *)fixedCopyLabel{
    if (_fixedCopyLabel == nil) {
        _fixedCopyLabel = [UILabel new];
        _fixedCopyLabel.textColor = COLOR_CONTENT;
        _fixedCopyLabel.fontNum = (12);
        _fixedCopyLabel.numberOfLines = 0;
        _fixedCopyLabel.lineSpace = (2);
        NSString * markStr = @"STP, Transfer o paynet";
        NSString * str = [NSString stringWithFormat:@"Nuestra aplicación solo acepta pagos a través de %@, por favor, asegúrese de la seguridad de sus fondos.", markStr];
        _fixedCopyLabel.width = self.fixedCopyView.width-LEFT_MARDIN*2;
        [_fixedCopyLabel setAttributedText:[str setDifferentColorWithMarkStr:markStr lineSpacing:2 alignment:NSTextAlignmentLeft color:COLOR_RED]];
        [_fixedCopyLabel sizeToFit];
    }
    return _fixedCopyLabel;
}
- (UILabel *)detailTitleLabel
{
    if (_detailTitleLabel == nil) {
        _detailTitleLabel = [UILabel  new];
        _detailTitleLabel.textColor = COLOR_BLACK;
        _detailTitleLabel.font = [UIFont boldSystemFontOfSize:(16)];
        [_detailTitleLabel fitTitle:@"Detalles de la factura" variable:0];
    }
    return  _detailTitleLabel;
}
- (UIButton *)openBtn
{
    if (_openBtn == nil) {
        _openBtn = [UIButton  new];
        [_openBtn setTitleColor:COLOR_CONTENT forState:UIControlStateNormal];
        _openBtn.titleLabel.fontNum = (14);
        NSString * str = @"Ocultar ";
        [_openBtn setAttributedTitle:[str setImageName:@"icon-arrow-down" imageRect:CGRectMake(0, -2, 14, 14) withIndex:str.length] forState:UIControlStateNormal];
        [_openBtn sizeToFit];
        _openBtn.widthHeight = XY(_openBtn.width, _openBtn.height+(5));
        _openBtn.selected = false;
        _openBtn.tag = 2;
        [_openBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return  _openBtn;
}
- (UIView *)lineView{
    if (!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = COLOR_LINE;
    }
    return _lineView;
}
- (UIView *)detailBackView
{
    if (_detailBackView == nil) {
        _detailBackView = [UIView  new];
        _detailBackView.hidden = true;
        _detailBackView.backgroundColor = [UIColor clearColor];
        _detailBackView.widthHeight = XY(self.backView.width, 0);
    }
    return  _detailBackView;
}

#pragma mark - 初始化
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.width = SCREEN_WIDTH;
        [self addSubView];
    }
    return self;
}

//添加subview
- (void)addSubView{
    [self addSubview:self.backView];
    [self.backView addSubview:self.bigImg];
    [self.backView addSubview:self.nameLabel];
    [self.backView addSubview:self.stateLabel];
    [self.backView addSubview:self.moneyTitleLabel];
    [self.backView addSubview:self.moneyLabel];
    [self.backView addSubview:self.timetitleLabel];
    [self.backView addSubview:self.timeLabel];
    [self.backView addSubview:self.btnOpen];
    
    [self.backView addSubview:self.detailBackView];
    [self.detailBackView addSubview:self.openBtn];
    [self.detailBackView addSubview:self.detailTitleLabel];
    [self.detailBackView addSubview:self.lineView];
    
    [self.backView addSubview:self.fixedCopyView];
    
    //初始化页面
    [self resetViewWithModelPostpone:nil repaymentDetailModel:nil orderDetailModel:nil];
}

#pragma mark - 点击事件
- (void)btnClick:(UIButton *)sender{
    [NSObject clickFeedback];
    switch (sender.tag) {
        case 1: // open
        {
            self.btnOpen.selected = true;
            [self resetViewWithRepaymentDetailModel:self.repaymentDetailModel orderDetailModel:self.orderDetailModel array:self.aryDatas];
            if(self.blockOpenBtnClick){
                self.blockOpenBtnClick();
            }
        }
            break;
        case 2: // close
        {
            self.btnOpen.selected = false;
            [self resetViewWithRepaymentDetailModel:self.repaymentDetailModel orderDetailModel:self.orderDetailModel array:self.aryDatas];
            if(self.blockOpenBtnClick){
                self.blockOpenBtnClick();
            }
        }
            break;
            
        default:
            break;
    }
}

#pragma mark - 刷新view
// 展期还款
- (void)resetViewWithModelPostpone:(ModelPostpone *)modelPostpone repaymentDetailModel:(ModelRepaymentDetail *)repaymentDetailModel orderDetailModel:(ModelOrderDetail *)orderDetailModel{
    
    self.backView.widthHeight = XY(SCREEN_WIDTH-2*LEFT_MARDIN, 0);
    UIImage * image = [UIImage new];
    if(isStr(readStr(orderDetailModel.slogan))){
        NSData *imageData = [[NSData alloc] initWithBase64EncodedString:orderDetailModel.slogan options:NSDataBase64DecodingIgnoreUnknownCharacters];
        image = [UIImage imageWithData:imageData];
    }
    self.bigImg.image = image;
    self.bigImg.leftTop = XY(LEFT_MARDIN, LEFT_MARDIN);
    
    [self.nameLabel fitTitle:readStr(orderDetailModel.productName) variable:0];
    self.nameLabel.leftCenterY = XY(self.bigImg.right+(12), self.bigImg.centerY);
    
    [self.stateLabel fitTitle:@"Prórroga de plazo" variable:0];
    self.stateLabel.widthHeight = XY(self.stateLabel.width+LEFT_MARDIN*2, 20);
    self.stateLabel.leftCenterY = XY(self.nameLabel.right+9, self.bigImg.centerY);
    
    [self.moneyTitleLabel fitTitle:@"Gasto de prórrog($)" variable:0];
    self.moneyTitleLabel.leftTop = XY(self.bigImg.left, self.bigImg.bottom+(16));
    [self.moneyLabel fitTitle:[GlobalMethod showMoney:strDot2F(repaymentDetailModel.amount)] variable:0];
    self.moneyLabel.leftTop = XY(self.moneyTitleLabel.left, self.moneyTitleLabel.bottom+12);
    
    [self.timetitleLabel fitTitle:@"Nueva fecha de vencimiento" variable:0];
    self.timetitleLabel.rightCenterY = XY(self.backView.width-self.bigImg.left, self.moneyTitleLabel.centerY);
    [self.timeLabel fitTitle:readStr(modelPostpone.delayExpiryTime) variable:0];
    self.timeLabel.rightCenterY = XY(self.timetitleLabel.right, self.moneyLabel.centerY);
    
    // 展期还款不显示 Detalles de la factura
    self.btnOpen.hidden = true;
    self.fixedCopyView.centerXTop = XY(self.backView.width/2, self.moneyLabel.bottom+LEFT_MARDIN);
    self.backView.height = self.fixedCopyView.bottom+LEFT_MARDIN;
    [GlobalMethod setRoundView:self.backView color:[UIColor clearColor] numRound:16 width:0];
    self.backView.leftTop = XY(LEFT_MARDIN, 0);
    self.height = self.backView.bottom+LEFT_MARDIN;
}

// 正常还款
- (void)resetViewWithRepaymentDetailModel:(ModelRepaymentDetail *)repaymentDetailModel orderDetailModel:(ModelOrderDetail *)orderDetailModel array:(NSArray *)array{
    self.repaymentDetailModel = repaymentDetailModel;
    self.orderDetailModel = orderDetailModel;
    self.aryDatas = array;
    
    self.backView.widthHeight = XY(SCREEN_WIDTH-2*LEFT_MARDIN, 0);
    UIImage * image = [UIImage new];
    if(isStr(readStr(orderDetailModel.slogan))){
        NSData *imageData = [[NSData alloc] initWithBase64EncodedString:orderDetailModel.slogan options:NSDataBase64DecodingIgnoreUnknownCharacters];
        image = [UIImage imageWithData:imageData];
    }
    self.bigImg.image = image;
    self.bigImg.leftTop = XY(LEFT_MARDIN, LEFT_MARDIN);
    
    [self.nameLabel fitTitle:readStr(orderDetailModel.productName) variable:0];
    self.nameLabel.leftCenterY = XY(self.bigImg.right+(12), self.bigImg.centerY);
    
    NSString * stateStr = @"Reembolso";
    // 立即还款就显示Reembolso，不考虑逾期不逾期了
//    if([orderDetailModel.status integerValue] == 180){
//        stateStr = @"Reembolso atrasado";
//    }
    [self.stateLabel fitTitle:stateStr variable:0];
    self.stateLabel.widthHeight = XY(self.stateLabel.width+LEFT_MARDIN*2, 20);
    self.stateLabel.leftCenterY = XY(self.nameLabel.right+9, self.bigImg.centerY);
    
    [self.moneyTitleLabel fitTitle:@"Monto de pago($)" variable:0];
    self.moneyTitleLabel.leftTop = XY(self.bigImg.left, self.bigImg.bottom+(16));
    [self.moneyLabel fitTitle:[GlobalMethod showMoney:strDot2F(repaymentDetailModel.amount)] variable:0];
    self.moneyLabel.leftTop = XY(self.moneyTitleLabel.left, self.moneyTitleLabel.bottom+12);
    
    [self.timetitleLabel fitTitle:@"Fecha de vencimiento" variable:0];
    self.timetitleLabel.rightCenterY = XY(self.backView.width-self.bigImg.left, self.moneyTitleLabel.centerY);
    [self.timeLabel fitTitle:orderDetailModel.expiryTime variable:0];
    self.timeLabel.rightCenterY = XY(self.timetitleLabel.right, self.moneyLabel.centerY);
    
    self.btnOpen.hidden = self.btnOpen.selected;
    self.btnOpen.rightTop = XY(self.timetitleLabel.right+2, self.timeLabel.bottom);
    self.detailBackView.hidden = !self.btnOpen.hidden;
    
    self.detailTitleLabel.leftTop = XY(LEFT_MARDIN, 0);
    self.openBtn.rightCenterY = XY(self.detailBackView.width-self.detailTitleLabel.left, self.detailTitleLabel.centerY);
    self.lineView.widthHeight = XY(self.detailBackView.width-self.detailTitleLabel.left*2, 1);
    self.lineView.leftTop = XY(self.detailTitleLabel.left, self.detailTitleLabel.bottom+12);
    
    CGFloat titleTop = self.lineView.bottom+26;
    [self.detailBackView removeAllSubViews];
    [self.detailBackView addSubview:self.detailTitleLabel];
    [self.detailBackView addSubview:self.openBtn];
    [self.detailBackView addSubview:self.lineView];
    if(isAry(array)){
        for (int i = 0; i < array.count; i ++) {
            ModelPairs * pairsItem = array[i];
            UILabel * titleLabel = [UILabel new];
            titleLabel.textColor = COLOR_CONTENT;
            titleLabel.fontNum = 12;
            [titleLabel fitTitle:pairsItem.key variable:0];
            titleLabel.leftTop = XY(self.detailTitleLabel.left, titleTop);
            [self.detailBackView addSubview:titleLabel];
            titleTop = titleLabel.bottom+(36);
            
            UILabel * contentLabel = [UILabel new];
            contentLabel.textColor = COLOR_BLACK;
            contentLabel.font = [UIFont boldSystemFontOfSize:14];
            [contentLabel fitTitle:pairsItem.value variable:0];
            contentLabel.rightCenterY = XY(self.detailBackView.width-self.detailTitleLabel.left, titleLabel.centerY);
            [self.detailBackView addSubview:contentLabel];
        }
    }
    
    self.detailBackView.height = titleTop-18;
    self.detailBackView.leftTop = XY(0, self.btnOpen.bottom);
    
    self.fixedCopyView.hidden = self.btnOpen.isSelected == true;
    self.fixedCopyView.centerXTop = XY(self.backView.width/2, self.btnOpen.hidden ? self.detailBackView.bottom + LEFT_MARDIN : self.btnOpen.bottom);
    
    if(self.btnOpen.isSelected == true){
        self.backView.height = self.detailBackView.bottom+10;
    }else{
        self.backView.height = self.fixedCopyView.bottom+LEFT_MARDIN;
    }
    
    [GlobalMethod setRoundView:self.backView color:[UIColor clearColor] numRound:16 width:0];
    self.backView.leftTop = XY(LEFT_MARDIN, 0);
    self.height = self.backView.bottom+LEFT_MARDIN;
}

@end
